package application

import (
	"cloud_android_backend/api/common"
	"cloud_android_backend/pkg/models/application"
	"github.com/gin-gonic/gin"
	"github.com/golang/glog"
	"github.com/jinzhu/gorm"
	"modules.tenxcloud.com/common/composite"
	"net/http"
)

func GetModelList(c *gin.Context) {
	db, err := composite.Database.GetOrm()
	if err != nil {
		glog.Errorf("Get model list error: %v", err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	models := []application.Model{}
	model := &application.Model{}
	models, err = model.GetList(db)
	if err != nil {
		if gorm.IsRecordNotFoundError(err) {
			c.JSON(http.StatusOK, common.SuccessResponse(models))
			return
		}
		glog.Errorf("Get model list error: %v", err)
		c.JSON(common.ErrorResponse(err.Error()))
		return
	}
	c.JSON(http.StatusOK, common.SuccessResponse(models))
}

func GetVoiceList(c *gin.Context) {
	db, err := composite.Database.GetOrm()
	if err != nil {
		glog.Errorf("Get voice list error: %v", err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	voiceList := []application.Voice{}
	voice := &application.Voice{}
	voiceList, err = voice.GetVoiceList(db)
	if err != nil {
		if gorm.IsRecordNotFoundError(err) {
			c.JSON(http.StatusOK, common.SuccessResponse(voiceList))
			return
		}
		glog.Errorf("Get voice list error: %v", err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
	}
	c.JSON(http.StatusOK, common.SuccessResponse(voiceList))
}
func GetKnowledgeList(c *gin.Context) {
	db, err := composite.Database.GetOrm()
	if err != nil {
		glog.Errorf("Get knowledge list error: %v", err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	knowledges := []application.Knowledge{}
	knowledge := &application.Knowledge{}
	knowledges, err = knowledge.GetList(db)
	if err != nil {
		if gorm.IsRecordNotFoundError(err) {
			c.JSON(http.StatusOK, common.SuccessResponse(knowledges))
			return
		}
		glog.Errorf("Get knowledge list error: %v", err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
	}
	c.JSON(http.StatusOK, common.SuccessResponse(knowledges))
}
